package com.iotproject.studyservice.controller;

import com.iotproject.commonutils.R;

import com.iotproject.studyservice.entity.vo.CollectionWord;
import com.iotproject.studyservice.entity.vo.TranslateSentence;
import com.iotproject.studyservice.entity.vo.WordDictionaryParts;
import com.iotproject.studyservice.service.TranslateService;
import com.iotproject.studyservice.utils.CastListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/studyservice/translate")
public class TranslateController {

    @Autowired
    TranslateService translateService;

    @PostMapping("sentenceTranslation")
    public R sentenceTranslation(@RequestBody(required = false) TranslateSentence translateSentence) throws IOException {
        Map<String, String> map = translateService.sentenceTranslation(translateSentence.getFrom(), translateSentence.getTo(), translateSentence.getContent());
        String dst = map.get("dst");
        return R.ok().data("dst", dst);
    }

    @PostMapping("wordDictionary/{wordName}")
    public R wordDictionary(@PathVariable("wordName") String wordName) throws IOException {
        Map<String, Object> map = translateService.wordDictionary(wordName);
        String dst = (String)(map.get("dst"));
        String ph_en = (String)(map.get("ph_en"));
        List<WordDictionaryParts> partsList = CastListUtils.castList(map.get("partsList"), WordDictionaryParts.class);
        return R.ok().data("dst", dst).data("ph_en", ph_en).data("partsList", partsList);
    }

    //在词典界面加入收藏夹操作
    @PostMapping("insert/collectionWordByWD")
    public R insertCollectionWordByWD(@RequestBody CollectionWord collectionWord, HttpServletRequest httpServletRequest) {
        translateService.insertCollectionWordByWD(collectionWord, httpServletRequest);
        return R.ok();
    }
    //在词典界面加入生词本操作
    @PostMapping("insert/unrememberWordByWD")
    public R insertUnrememberWordByWD(@RequestBody CollectionWord collectionWord, HttpServletRequest httpServletRequest) {
        translateService.insertUnrememberWordByWD(collectionWord, httpServletRequest);
        return R.ok();
    }

}

